﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.ComponentModel;
using TA.JSON;
using TA.Win.Utils.Design;
using System.Drawing.Design;


namespace TA.Turbo.Controls
{
	/// <summary>
	/// Specifies settings for displaying the pagers in multi-page controls.
	/// </summary>
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public class PagerSettings : IJsonCompatible
	{
		/// <summary>
		/// Ctor.
		/// </summary>
		public PagerSettings()
		{
			Position = PagerPosition.Top;
			ButtonCount = 10;
			this.ButtonHref = "";
			this.ButtonHrefJS = "";
		}
		/// <summary>
		/// The default HREF to be used for all page buttons.
		/// </summary>
		[NotifyParentProperty(true)]
		[DefaultValue("")]
		public string ButtonHref { get; set; }
		/// <summary>
		/// Teh default JavaScript-enabled HREF to be used for all page buttons.
		/// </summary>
		[NotifyParentProperty(true)]
		[DefaultValue("")]
		public string ButtonHrefJS { get; set; }
		/// <summary>
		/// Specifies the styles that are applied to the first page button.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Editor(typeof(PropertyEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public FirstPageElement First { get { return _first; } }
		/// <summary>
		/// Specifies the styles that are applied to the previous page button.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Editor(typeof(PropertyEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public PreviousGroupElement PreviousGroup { get { return this._previousGroup; } }
		/// <summary>
		/// Specifies the styles that are applied to the previous page button.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Editor(typeof(PropertyEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public PreviousPageElement Previous { get { return _previous; } }
		/// <summary>
		/// Specifies the styles that are applied to an indexed page button.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Editor(typeof(PropertyEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public NormalPageElement Normal { get { return this._normal; } }
		/// <summary>
		/// Specifies the styles that are applied to the next page button.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Editor(typeof(PropertyEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public NextPageElement Next { get { return _next; } }
		/// <summary>
		/// Specifies the styles that are applied to the next page button.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Editor(typeof(PropertyEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public NextGroupElement NextGroup { get { return this._nextGroup; } }
		/// <summary>
		/// Specifies the styles that are applied to the last page button.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[Editor(typeof(PropertyEditor), typeof(UITypeEditor))]
		[NotifyParentProperty(true)]
		public LastPageElement Last { get { return _last; } }
		/// <summary>
		/// The number of items to be displayed on a single page. If set to 0, paging is off.
		/// </summary>
		[DefaultValue(0)]
		[NotifyParentProperty(true)]
		public int Size { get; set; }
		/// <summary>
		/// The maximum number of buttons to be shown at the same time (for ex., ... 4 5 6 7 8 9 10 ...).
		/// </summary>
		[DefaultValue(10)]
		[NotifyParentProperty(true)]
		public int ButtonCount { get; set; }
		/// <summary>
		/// Specifies where is the pager displayed (top, bottom or both).
		/// </summary>
		[DefaultValue(PagerPosition.Top)]
		[NotifyParentProperty(true)]
		public PagerPosition Position { get; set; }
		/// <summary>
		/// Controls how the pager's visibility is controlled.
		/// </summary>
		[DefaultValue(PagerDisplayMode.ShowAlways)]
		[NotifyParentProperty(true)]
		public PagerDisplayMode DisplayMode { get; set; }

		#region INTERNAL
		#region VARS
		FirstPageElement _first = new FirstPageElement();
		PreviousGroupElement _previousGroup = new PreviousGroupElement();
		PreviousPageElement _previous = new PreviousPageElement();
		NormalPageElement _normal = new NormalPageElement();
		NextPageElement _next = new NextPageElement();
		NextGroupElement _nextGroup = new NextGroupElement();
		LastPageElement _last = new LastPageElement();
		#endregion

		#region JSON
		/// <summary>
		/// Gets true if any of the properties are dirty.
		/// </summary>
		[Browsable(false)]
		public bool IsDirty
		{
			get
			{
				return
					this.First.IsDirty
					||
					this.Previous.IsDirty
					||
					this.PreviousGroup.IsDirty
					||
					this.NextGroup.IsDirty
					||
					this.Next.IsDirty
					||
					this.Last.IsDirty
					||
					this.Normal.IsDirty
					||
					this.Size != 0
					||
					this.ButtonCount != 10
					||
					this.Position != PagerPosition.Top
					||
					this.DisplayMode != PagerDisplayMode.ShowAlways
					||
					!string.IsNullOrEmpty(this.ButtonHref)
					||
					!string.IsNullOrEmpty(this.ButtonHrefJS);
			}
		}
		/// <summary>
		/// Serializes this object.
		/// </summary>
		/// <param name="jw"></param>
		public void Serialize(JsonWriter jw)
		{
			this.SerializeObject(jw);
		}
		/// <summary>
		/// SerializeContents.
		/// </summary>
		/// <param name="jw"></param>
		public void SerializeContents(JsonWriter jw)
		{
			if (First.IsDirty)
			{
				jw.PropertyName("F");
				First.Serialize(jw);
			}

			if (Previous.IsDirty)
			{
				jw.PropertyName("P");
				Previous.Serialize(jw);
			}

			if (this.PreviousGroup.IsDirty)
			{
				jw.PropertyName("PG");
				this.PreviousGroup.Serialize(jw);
			}

			if (this.NextGroup.IsDirty)
			{
				jw.PropertyName("NG");
				this.NextGroup.Serialize(jw);
			}

			if (Next.IsDirty)
			{
				jw.PropertyName("N");
				Next.Serialize(jw);
			}

			if (Last.IsDirty)
			{
				jw.PropertyName("L");
				Last.Serialize(jw);
			}

			if (this.Normal.IsDirty)
			{
				jw.PropertyName("R");
				this.Normal.Serialize(jw);
			}

			if (Size != 0)
			{
				jw.PropertyName("S");
				jw.Value(Size);
			}

			if (ButtonCount != 10)
			{
				jw.PropertyName("B");
				jw.Value(ButtonCount);
			}

			if (this.Position != PagerPosition.Top)
			{
				jw.PropertyName("O");
				jw.Value(this.Position);
			}

			if (this.DisplayMode != PagerDisplayMode.ShowAlways)
			{
				jw.PropertyName("M");
				jw.Value(this.DisplayMode);
			}

			if (!string.IsNullOrEmpty(this.ButtonHref))
			{
				jw.PropertyName("H");
				jw.Value(this.ButtonHref);
			}

			if (!string.IsNullOrEmpty(this.ButtonHrefJS))
			{
				jw.PropertyName("J");
				jw.Value(this.ButtonHrefJS);
			}

		}
		/// <summary>
		/// Deserializes this object.
		/// </summary>
		/// <param name="jr"></param>
		public void Deserialize(IJsonReader jr)
		{
			this.DeserializeObject(jr);

		}
		/// <summary>
		/// DeserializeContent.
		/// </summary>
		/// <param name="jr"></param>
		/// <param name="prShortName"></param>
		public void DeserializeContent(IJsonReader jr, string prShortName)
		{
			switch (prShortName)
			{
				case "F": this.First.Deserialize(jr); break;
				case "P": this.Previous.Deserialize(jr); break;
				case "PG": this.PreviousGroup.Deserialize(jr); break;

				case "NG": this.NextGroup.Deserialize(jr); break;
				case "N": this.Next.Deserialize(jr); break;
				case "L": this.Last.Deserialize(jr); break;

				case "R": this.Normal.Deserialize(jr); break;

				case "S": this.Size = (int)(long)jr.Value; break;
				case "B": this.ButtonCount = (int)(long)jr.Value; break;
				case "O": this.Position = (PagerPosition)(long)jr.Value; break;
				case "M": this.DisplayMode = (PagerDisplayMode)(long)jr.Value; break;

				case "H": this.ButtonHref = (string)jr.Value; break;
				case "J": this.ButtonHrefJS = (string)jr.Value; break;
			}
		}

		#endregion
		#endregion
	}
}